//
//  BaseViewController.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/17.
//

import UIKit
import SnapKit
import Kingfisher
import RxSwift
import RxCocoa

open class BaseViewController: NiblessViewController, UIGestureRecognizerDelegate, CTNavViewDelegate {
    
    lazy var bag = DisposeBag()
    
    let navBar = CTNavView()
    override open func viewDidLoad() {
        super.viewDidLoad()
        navBar.delegate = self
        view.addSubview(navBar)
        view.backgroundColor = .color(.viewBg)
        self.navigationItem.title = ""
        if #available(iOS 11.0, *) {
//            _scrollView.contentInsetAdjustmentBehavior = .never;
        }else {
            self.automaticallyAdjustsScrollViewInsets = false;
        }
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
    }
    
    override open func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        if isLoadAnimate {
            stopAnimating()
        }
    }
    
    deinit {
        k_logViewControllerDeinit(self)
    }
    
    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        KingfisherManager.shared.cache.clearMemoryCache()
    }
    
    override open var preferredStatusBarStyle: UIStatusBarStyle {
        if #available(iOS 13.0, *) {
            return .darkContent
        } else {
            return .default
        }
    }
    
    /// Animation indicator
    fileprivate lazy var isLoadAnimate: Bool = { return false } ()
    lazy var animateView: UIActivityIndicatorView = {
        let indicator = UIActivityIndicatorView()
        if #available(iOS 13.0, *) {
            indicator.style = .gray
        } else {
            indicator.style = .gray
        }
        indicator.centerX = self.view.centerX
        indicator.centerY = self.view.centerY - 30.fitC
        self.isLoadAnimate = true
        self.view.addSubview(indicator)
        return indicator
    }()
}

extension BaseViewController {
    @objc public func navleftViewTapped() {
        close()
    }
    
    @objc public func navRightViewTapped() {
        
    }
    
    @discardableResult
    public func setNavLeft(icon: UIImage?) -> UIButton {
        navBar.leftView.setImage(icon, for: .normal)
        return navBar.leftView
    }
    
    @discardableResult
    public func setNavLeft(title: String,
                           titleColor: UIColor = .color(.navTitle),
                           titleFont: UIFont = .regular(14, isFit: false)) -> UIButton {
        navBar.leftView.setImage(nil, for: .normal)
        navBar.leftView.setTitleColor(titleColor, for: .normal)
        navBar.leftView.titleLabel?.font = titleFont
        navBar.leftView.setTitle(title, for: .normal)
        return navBar.leftView
    }
}

// MARK: - ********* View loading animating
public extension BaseViewController {
    
    func startAnimating(at centerY: CGFloat? = nil) {
        if let centerY = centerY {
            animateView.center = CGPoint(x: kScreenWid.half, y: centerY)
        }
        animateView.superview?.bringSubviewToFront(animateView)
        animateView.startAnimating()
    }
    
    func stopAnimating() {
        animateView.stopAnimating()
    }
    
    var isAnimating: Bool {
        return animateView.isAnimating
    }

    func close() {
        if let _ = presentingViewController {
            dismiss(animated: true, completion: nil)
        } else if let nav = navigationController {
            nav.popViewController(animated: true)
        }
    }
}



